import torch
from transformers import BertTokenizer, BertModel
# BERT_PATH = 'E:/models/bert'#bert-base-uncased
BERT_PATH = './bert-base-uncased'#bert-base-uncased
# 加载BERT模型和tokenizer
tokenizer = BertTokenizer.from_pretrained(BERT_PATH)
model = BertModel.from_pretrained(BERT_PATH)

# 输入文本
text = "the medium sized bird has a dark grey color, a black downward curved beak, and long wings"

# 将文本转换为BERT需要的输入格式
input_ids = torch.tensor(tokenizer.encode(text, add_special_tokens=True)).unsqueeze(0)

# 使用BERT模型提取文本特征
outputs = model(input_ids)
word_embeddings = outputs[0]#outputs.last_hidden_state  # 每个单词的词向量
print(word_embeddings)
sentence_embedding = outputs[1]#outputs.pooler_output  # 整个句子的句子向量
print(sentence_embedding)